package com.baitengsoft.patentapimonitor.api.controller;

import com.baitengsoft.patentapimonitor.api.entity.LogUserinfoHistoryEntity;
import com.baitengsoft.patentapimonitor.api.entity.WebUserinfoEntity;
import com.baitengsoft.patentapimonitor.api.model.SearchPage;
import com.baitengsoft.patentapimonitor.api.model.dto.WebDto;
import com.baitengsoft.patentapimonitor.api.service.LogUserinfoHistoryService;
import com.baitengsoft.patentapimonitor.api.service.WebUserService;
import com.baitengsoft.patentapimonitor.common.utils.ExcelUtil;
import com.baitengsoft.patentapimonitor.common.utils.PageResult;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * Created by Admin on 2017/12/28.
 */
@RestController
@RequestMapping("api/v1/admin")
@Api(value = "后台-系统日志",description = "系统日志")
public class AdminSystemLogController {

    @Autowired
    WebUserService webUserService;
    @Autowired
    LogUserinfoHistoryService logUserinfoHistoryService;

    @ApiOperation(value = "用户列表")
    @RequestMapping(value="/searchuserinforforsys", method= RequestMethod.POST)
    public Res searchUserInforForSys(@RequestBody SearchPage param){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            PageResult<WebUserinfoEntity> result= webUserService.searchUserInfo(param);
            if(result!=null){
                flag=true;
                map.put("data",result);
            }else{
                map.put("msg","暂无数据");
            }
        } catch (Exception e) {
            map.put("msg","获取数据异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }


    @ApiOperation(value = "用户历史数据列表")
    @RequestMapping(value="/searchuserhistory", method= RequestMethod.POST)
    public Res searchUserHistory(@RequestBody SearchPage param, @RequestParam("userName") String userName){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            PageResult<LogUserinfoHistoryEntity> result= logUserinfoHistoryService.searchHistoryLog(userName,param);
            if(result!=null){
                flag=true;
                map.put("data",result);
            }else{
                map.put("msg","暂无数据");
            }
        } catch (Exception e) {
            map.put("msg","获取数据异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }

    /**
     * new FileOutputStream("D:/temp/out.xls")
     * @param idStr
     * @param response
     * @return
     */
    @ApiOperation(value = "导出用户列表")
    @RequestMapping(value="/exportexcelforuser", method= RequestMethod.GET)
    public Res exportExcelForUser(@RequestParam("ids") String idStr, HttpServletRequest request, HttpServletResponse response){


        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            List<WebDto> webDtos=webUserService.searchUserInfoByIds(idStr);
            if(webDtos==null){
                map.put("msg","暂无数据");
            }else {
                Map<String, String> mapExcel = webUserService.getMapExcel(webDtos);
                String agent = request.getHeader("USER-AGENT");
//                String fileName="AAA";
//                if (null != agent && -1 != agent.indexOf("MSIE") || null != agent && -1 != agent.indexOf("Trident")) {// ie
//                    fileName = java.net.URLEncoder.encode(fileName, "UTF8");
//                } else if (null != agent && -1 != agent.indexOf("Mozilla")) {// 火狐,chrome等
//                    fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
//                }
                //fileName += ".xls";
//                response.setHeader("Content-disposition", "attachment;filename=\"" + fileName + "\"");
//                response.setContentType("application/vnd.ms-excel");
                String fileName="区域监控-用户表.xls";
                fileName=new String(fileName.getBytes("gb2312"), "iso8859-1");
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("Content-disposition", "attachment;filename=" + fileName);
                ExcelUtil.getInstance().exportObj2ExcelByTemplate(mapExcel, "web-info-template.xls",response.getOutputStream() ,
                        webDtos, WebDto.class, true);
            }
        } catch (Exception e) {
            map.put("msg","导出异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }






    @ApiOperation(value = "导出用户历史列表")
    @RequestMapping(value="/exportexceluserhistory", method= RequestMethod.GET)
    public Res exportExcelUserHistory(@RequestParam("ids") String idStr, HttpServletRequest request, HttpServletResponse response){


        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            List<WebDto> webDtos=logUserinfoHistoryService.searchById(idStr);
            if(webDtos==null){
                map.put("msg","暂无数据");
            }else {
                Map<String, String> mapExcel = webUserService.getMapExcel(webDtos);
                String agent = request.getHeader("USER-AGENT");
//                String fileName="AAA";
//                if (null != agent && -1 != agent.indexOf("MSIE") || null != agent && -1 != agent.indexOf("Trident")) {// ie
//                    fileName = java.net.URLEncoder.encode(fileName, "UTF8");
//                } else if (null != agent && -1 != agent.indexOf("Mozilla")) {// 火狐,chrome等
//                    fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
//                }
                //fileName += ".xls";
//                response.setHeader("Content-disposition", "attachment;filename=\"" + fileName + "\"");
//                response.setContentType("application/vnd.ms-excel");
                String fileName="区域监控-用户历史表.xls";
                fileName=new String(fileName.getBytes("gb2312"), "iso8859-1");
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("Content-disposition", "attachment;filename=" + fileName);
                ExcelUtil.getInstance().exportObj2ExcelByTemplate(mapExcel, "web-info-template.xls",response.getOutputStream() ,
                        webDtos, WebDto.class, true);
            }
        } catch (Exception e) {
            map.put("msg","导出异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }


//    @ApiOperation(value = "申请数据导入")
//    @ResponseBody
//    @PostMapping(value = "importxlsfile",consumes = "multipart/*",headers = "content-type=multipart/form-data")
//    public Res importXlsFile(@ApiParam(value = "上传文件",required = true)
//                             @RequestParam(value = "file", required = true) MultipartFile file) throws Exception {
//        if(file == null || !file.getOriginalFilename().contains(".xls")){
//            return Res.error("文件格式有误,请上传.xls文件");
//        }
//        return  null;
//    }





}
